<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px"
    >
      <el-form-item label="应用信息" prop="app_id">
        <el-select
          v-model="queryParams.app_id"
          placeholder="请选择应用信息"
        >
          <el-option
            v-for="item in appOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.app_customer_income_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" icon="el-icon-search" size="mini"
          v-hasPermi="['app:customerIncome:query']" @click="handleQuery"
        >搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:customerIncome:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="用户名" align="center" prop="name" :show-overflow-tooltip="true"
      />
      <el-table-column label="收益金额" align="center" prop="amount" :show-overflow-tooltip="true"
      />
      <el-table-column label="收益类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_customer_income_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" key="create_time" prop="create_time"
                       width="160"
      />
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:customerIncome:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:customerIncome:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 添加或修改平台滚动通知配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用信息" prop="app_id">
          <el-select
            v-model="form.app_id"
            placeholder="请选择应用信息"
            :disabled="form.id != undefined"
          >
            <el-option
              v-for="item in appOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名称" maxlength="30"/>
        </el-form-item>
        <el-form-item label="收益金额" prop="amount">
          <el-input type="number" step="0.01" v-model="form.amount" placeholder="请输入收益金额" maxlength="30"/>
        </el-form-item>
        <el-form-item label="收益类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.app_customer_income_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <span style="color: red">数量大于1的话，会在该用户名称下生成多条不通金额的数据</span>
        <el-form-item label="数量" prop="num">
          <el-input type="number" v-model="form.num" placeholder="请输入数量" maxlength="30"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAppCustomerIncome,
  delAppCustomerIncome,
  getAppCustomerIncome,
  listAppCustomerIncome,
  updateAppCustomerIncome
} from '@/api/metadata/appCustomerIncome'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {getGroupApp} from "@/api/metadata/app";
import form from "element-ui/packages/form";

export default {
  name: 'AppCustomerIncome',
  dicts: ['sys_normal_disable', "app_customer_income_type"],
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台滚动通知表格数据
      dataList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 应用信息
      appOptions: [],
      loadingInstance: null,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        app_id: undefined,
        name: undefined,
        type: undefined,
      },
      // 表单校验
      rules: {
        app_id: [
          {required: true, message: '应用信息不能为空', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '用户名次不能为空', trigger: 'blur'}
        ],
        amount: [
          {required: true, message: '收益金额不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '收益类型不能为空', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.getGroupApp()
  },
  methods: {
    getGroupApp() {
      getGroupApp().then(response => {
        this.appOptions = response.data
        this.queryParams.app_id = response.data[0].id
        this.getList()
      })
    },
    /** 查询平台滚动通知列表 */
    getList() {
      this.loading = true
      listAppCustomerIncome(this.queryParams).then(response => {
          this.dataList = response.data
          this.total = response.total
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        app_id: undefined,
        name: undefined,
        amount: undefined,
        type: 1,
        num: 1,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.form.app_id = this.queryParams.app_id
      this.open = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.form.app_id = this.queryParams.app_id
      getAppCustomerIncome(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          let step = async () => {
            if (this.form.id != undefined) {
              updateAppCustomerIncome(this.form).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addAppCustomerIncome(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
          step()
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return delAppCustomerIncome(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
